<template>
  <div class="vaccine pad">
    <div class="head">
      <span>疫苗管理</span>
      <el-button type="primary" circle icon="el-icon-plus" @click="openAddDialog">
      </el-button>
    </div>
    <div class="select m-t-b">
      <span style="padding-right: 20px;">筛选条件:</span>
      <el-radio-group v-model="isDefault" @change="searchByDefault">
        <el-radio :label="1">必打疫苗</el-radio>
        <el-radio :label="0">非必打疫苗</el-radio>
      </el-radio-group>
      <el-button
        style="margin-left: 30px;"
        size="mini"
        type="plain"
        @click="clearSearch"
        v-show="isDefault === 0 || isDefault === 1"
      >清除筛选</el-button>
    </div>
    <el-table class="m-t-b" border stripe :data="vaccines" v-loading="loading">
      <el-table-column label="疫苗名称" prop="vaccineName"></el-table-column>
      <el-table-column label="疫苗剂别" prop="code"></el-table-column>
      <el-table-column label="疫苗简介" prop="common"></el-table-column>
      <el-table-column label="疫苗功效" prop="detail"></el-table-column>
      <el-table-column label="适合月龄" prop="month"></el-table-column>
      <el-table-column label="是否必打">
        <template slot-scope="scope">
          {{scope.row.isdefault === "1" ? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="openEditDialog(scope.row)" type="default" icon="el-icon-edit" circle></el-button>
          <el-button @click="deleteVaccine(scope.row)" type="danger" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="flex m-t-b"
      :page-size="pageSize"
      layout="prev, pager, next"
      @current-change="handlePagi"
      :total="total">
    </el-pagination>
    <el-dialog :visible.sync="showAddDialog" :title="dialogTitle">
      <el-form label-position="top" :model="vaccine" ref="vaccine" :rules="rules">
        <el-form-item label="疫苗名称" prop="vaccineName">
          <el-input v-model="vaccine.vaccineName" />
        </el-form-item>
        <el-form-item label="疫苗剂别" prop="code">
          <el-input v-model="vaccine.code" />
        </el-form-item>
        <el-form-item label="疫苗简介" prop="common">
          <el-input v-model="vaccine.common" />
        </el-form-item>
        <el-form-item label="疫苗功效" prop="detail">
          <el-input v-model="vaccine.detail" />
        </el-form-item>
        <el-form-item label="适合月龄(月)" prop="month">
          <el-input type="number" v-model="vaccine.month" />
        </el-form-item>
        <el-form-item label="是否必打" prop="isdefault">
          <el-select v-model="vaccine.isdefault">
            <el-option label="必打疫苗" :value="1"></el-option>
            <el-option label="非必打疫苗" :value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hideDialog">取 消</el-button>
        <el-button type="primary" @click="addVaccine('vaccine')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addVaccine,
  getVaccineList,
  deleteVaccine,
  updateVaccine
} from '../api'

export default {
  name: 'vaccine',
  data () {
    return {
      isdefaultName: '',
      showAddDialog: false,
      pageSize: 10,
      dialogTitle: '添加疫苗',
      isDefault: '',
      total: 0,
      loading: true,
      vaccine: {
        vaccineName: '',
        code: '',
        common: '',
        detail: '',
        month: '',
        isdefault: 0
      },
      vaccines: [],
      rules: {
        vaccineName: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        common: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        detail: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        month: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    hideDialog () { // 隐藏弹出框
      this.showAddDialog = false
    },
    addVaccine (name) { // 添加疫苗
      this.$refs[name].validate(valid => {
        if (valid) {
          if (this.dialogTitle === '添加疫苗') {
            addVaccine({
              vaccineName: this.vaccine.vaccineName,
              code: this.vaccine.code,
              common: this.vaccine.common,
              detail: this.vaccine.detail,
              month: Number(this.vaccine.month),
              isdefault: this.vaccine.isdefault
            })
            .then(res => {
              if (res.data.code === 1) {
                this.fetchVaccineList(1);
                this.$message({
                  type: 'success',
                  message: '添加成功!'
                });
                this.showAddDialog = false
              } else {
                this.$message.error(res.data.msg || '网络异常!')
              }
            })
            .catch(e => {
              this.$message.error('网络异常!')
            })
          } else {
            updateVaccine({
              vaccineId: this.vaccine.vaccineId,
              vaccineName: this.vaccine.vaccineName,
              code: this.vaccine.code,
              common: this.vaccine.common,
              detail: this.vaccine.detail,
              month: this.vaccine.month,
              isdefault: this.vaccine.isdefault
            })
            .then(res => {
              if (res.data.code === 1) {
                this.fetchVaccineList(1);
                this.showAddDialog = false;
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                })
              } else {
                this.$message.error(res.data.msg || '网络异常!')
              }
            })
            .catch(e => {
              this.$message.error('网络异常!')
            })
          }
        } else {
          return false
        }
      })
    },
    openAddDialog () { // 打开添加弹出框
      if (!this.$checkAuth('canAddVaccine')) {
        return this.$message.error('暂无对应操作权限!')
      };
      this.dialogTitle = '添加疫苗';
      this.vaccine.vaccineName = '';
      this.vaccine.code = '';
      this.vaccine.common = '';
      this.vaccine.detail = '';
      this.vaccine.month = '';
      this.vaccine.isdefault = 0;
      this.showAddDialog = true
    },
    openEditDialog (data) { // 打开编辑弹出框
      if (!this.$checkAuth('canEditVaccine')) {
        return this.$message.error('暂无对应操作权限!')
      };
      this.dialogTitle = '修改疫苗';
      this.vaccine.vaccineName = data.vaccineName;
      this.vaccine.code = data.code;
      this.vaccine.common = data.common;
      this.vaccine.detail = data.detail;
      this.vaccine.month = data.month;
      this.vaccine.isdefault = Number(data.isdefault);
      if(data.isdefault == '1'){
        this.isdefaultName == '必打疫苗'
      }else{
        console.log('222')
      }
      this.vaccine.vaccineId = data.vaccineId
      this.showAddDialog = true
    },
    handlePagi (page) { // 分页
      if (this.isDefault === 0 || this.isDefault === 1) {
        this.fetchVaccineList(page, this.isDefault)
      } else {
        this.fetchVaccineList(page)
      }
    },
    searchByDefault () { // 根据类型筛选
      this.fetchVaccineList(1, this.isDefault)
    },
    deleteVaccine (data) { // 删除疫苗
      if (!this.$checkAuth('canDeleteVaccine')) {
        return this.$message.error('暂无对应操作权限!')
      };
      this.$confirm(`确定将疫苗: "${data.vaccineName}" 删除吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteVaccine(data.vaccineId)
        .then(res => {
          if (res.data.code === 1) {
            this.fetchVaccineList(1);
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          } else {
            this.$message.error(res.data.msg || '网络异常!')
          }
        })
        .catch(e => {
          this.$message.error('网络异常!')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消操作'
        })
      });
    },
    clearSearch () {
      this.isDefault = '';
      this.fetchVaccineList(1)
    },
    fetchVaccineList (index, isdefault) { // 请求疫苗列表
      this.loading = true;
      if (isdefault !== 0 && !isdefault) {
        isdefault = null
      };
      getVaccineList({
        index,
        count: this.pageSize,
        queryParam: {
          isdefault: isdefault
        }
      })
      .then(res => {
        if (res.data.code === 1) {
          let list = res.data.data.list;
          this.total = res.data.data.totalRecord;
          this.vaccines = list

        } else {
          this.$message.error(res.data.msg || '网络异常!')
        };
        this.loading = false
      })
      .catch(e => {
        this.$message.error('网络异常!');
        this.loading = false
      })
    }
  },
  created () {
    this.fetchVaccineList(1)
  }
}
</script>
<style lang="scss">
  .vaccine {
    table {
      .cell {
        text-align: center;
      }
    }
    .select {
      display: flex;
      align-items: center;
      background-color: #F2F2F2;
      border-radius: 10px;
      padding: 10px 20px;
    }
  }
</style>
